//
//  MeetingSelectViewController.m
//  YUNWUMeetingDemo
//
//  Created by mac on 2019/7/15.
//  Copyright © 2019 DGL. All rights reserved.
//

#import "MeetingSelectViewController.h"

@interface MeetingSelectViewController ()<UIPickerViewDataSource, UIPickerViewDelegate>

@property (nonatomic, strong) UIPickerView *pickerView;
/** 当前选中pickerView的row */
@property (nonatomic, assign) NSInteger selectedRow;

@end

@implementation MeetingSelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpAllHeaderView];
    // Do any additional setup after loading the view.
}

#pragma mark -- UIPickerViewDelegate && DataSource
//UIPickerViewDataSource中定义的方法，该方法的返回值决定该控件包含的列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
    return 1;
}
//UIPickerViewDataSource中定义的方法，该方法的返回值决定该控件指定列包含多少个列表项
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _dataSource.count;
}

// UIPickerViewDelegate中定义的方法，该方法返回的NSString将作为UIPickerView
// 中指定列和列表项的标题文本
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    // 由于该控件只包含一列，因此无须理会列序号参数component
    // 该方法根据row参数返回teams中的元素，row参数代表列表项的编号，
    // 因此该方法表示第几个列表项，就使用teams中的第几个元素
    return [_dataSource objectAtIndex:row];
}

// 当用户选中UIPickerViewDataSource中指定列和列表项时激发该方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _selectedRow = row;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *myView = nil;
    
    if (component == 0) {
        myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width/2, 30)];
        myView.textAlignment = NSTextAlignmentCenter;
        myView.text = [_dataSource objectAtIndex:row];
        myView.textColor = [UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1];
        myView.font = [UIFont systemFontOfSize:14];         //用label来设置字体大小
        myView.backgroundColor = [UIColor clearColor];
    }
    return myView;
}
// UIPickerViewDelegate中定义的方法，该方法返回的NSString将作为// UIPickerView中指定列的宽度
-(CGFloat)pickerView:(UIPickerView *)pickerView   widthForComponent:(NSInteger)component
{
    return 100;
    //宽度为100
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 40;
}

#pragma mark -- 界面UI
- (void)setUpAllHeaderView
{
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, +10, self.view.frame.size.width, 350)];
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 42)];
    view.backgroundColor = [UIColor whiteColor];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [button setImage:[UIImage imageWithName:@"t_close"] forState:UIControlStateNormal];
    [button setTitle:@"完成" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14.0f];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(view.frame.size.width-45, view.frame.origin.y+10, 30, 30);
    [backView addSubview:button];
    
    _pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(view.frame)+5, self.view.frame.size.width, backView.frame.size.height-10)];
    
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    _pickerView.backgroundColor = [UIColor whiteColor];
    /**
     *  显示选中框
     */
    _pickerView.showsSelectionIndicator = YES;
    _pickerView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; //这里设置了就可以自定                                                                                                                           义高度了，一般默认是无法修改其216像素的高度
    [backView addSubview:_pickerView];

    _selectedRow = 50;
    
    [_pickerView selectRow:_selectedRow inComponent:0 animated:NO];
    if (self.dataSource.count > 1 ) {
        _selectedRow = self.dataSource.count/2 - 1;
        [_pickerView selectRow:self.dataSource.count/2 - 1 inComponent:0 animated:NO];
    }
    
}

//x按钮：点击x按钮时，弹出界面消失，下面的去结算view出现
- (void)btnAction: (UIButton *)sender
{
    if (self.MeetingSelectViewBalck) {
        self.MeetingSelectViewBalck([NSMutableString stringWithFormat:@"%@",[_dataSource objectAtIndex:_selectedRow]]);
    }
    
    //弹出界面消失
    [self dismissViewControllerAnimated:NO completion:^{
    }];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"showFootView" object:nil];

}


@end
